<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CatMerge - 猫咪合并游戏</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Arial', sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .game-container {
            max-width: 800px;
            margin: 0 auto;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 20px;
            padding: 20px;
            backdrop-filter: blur(10px);
            box-shadow: 0 8px 32px rgba(31, 38, 135, 0.37);
        }

        .header {
            text-align: center;
            margin-bottom: 20px;
            color: white;
        }

        .stats {
            display: flex;
            justify-content: space-around;
            margin-bottom: 20px;
            background: rgba(255, 255, 255, 0.2);
            padding: 15px;
            border-radius: 15px;
            color: white;
        }

        .stat-item {
            text-align: center;
        }

        .stat-value {
            font-size: 24px;
            font-weight: bold;
            color: #FFD700;
        }

        .game-board {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 10px;
            margin-bottom: 20px;
        }

        .cat-slot {
            aspect-ratio: 1;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s ease;
            border: 2px solid transparent;
            position: relative;
        }

        .cat-slot:hover {
            transform: scale(1.05);
            background: rgba(255, 255, 255, 0.3);
        }

        .cat-slot.selected {
            border-color: #FFD700;
            box-shadow: 0 0 15px rgba(255, 215, 0, 0.5);
        }

        .cat {
            width: 80%;
            height: 80%;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 30px;
            font-weight: bold;
            color: white;
            position: relative;
            transition: all 0.3s ease;
        }

        .cat-level {
            position: absolute;
            bottom: -5px;
            right: -5px;
            background: #FFD700;
            color: #333;
            border-radius: 50%;
            width: 25px;
            height: 25px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 12px;
            font-weight: bold;
        }

        .controls {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-bottom: 20px;
        }

        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 25px;
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }

        .merge-animation {
            animation: merge 0.5s ease-in-out;
        }

        @keyframes merge {
            0% {
                transform: scale(1);
                opacity: 1;
            }
            50% {
                transform: scale(1.2);
                opacity: 0.7;
            }
            100% {
                transform: scale(0);
                opacity: 0;
            }
        }

        .new-cat-animation {
            animation: appear 0.5s ease-in-out;
        }

        @keyframes appear {
            0% {
                transform: scale(0);
                opacity: 0;
            }
            100% {
                transform: scale(1);
                opacity: 1;
            }
        }

        .settings {
            position: fixed;
            top: 20px;
            right: 20px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            width: 50px;
            height: 50px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            color: white;
            font-size: 20px;
        }

        .settings:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            z-index: 1000;
        }

        .modal-content {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 30px;
            border-radius: 20px;
            text-align: center;
            max-width: 400px;
            width: 90%;
        }

        .close {
            position: absolute;
            top: 10px;
            right: 15px;
            font-size: 24px;
            cursor: pointer;
            color: #666;
        }

        .offline-earnings {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: rgba(255, 215, 0, 0.9);
            padding: 20px;
            border-radius: 15px;
            text-align: center;
            display: none;
            z-index: 1001;
        }
    </style>
</head>
<body>
    <div class="settings" onclick="openSettings()">⚙️</div>
    
    <div class="game-container">
        <div class="header">
            <h1>🐱 CatMerge</h1>
            <p>合并相同的猫咪，解锁更高级的猫咪！</p>
        </div>

        <div class="stats">
            <div class="stat-item">
                <div>最高等级</div>
                <div class="stat-value" id="maxLevel">1</div>
            </div>
            <div class="stat-item">
                <div>猫咪总数</div>
                <div class="stat-value" id="totalCats">2</div>
            </div>
            <div class="stat-item">
                <div>金币</div>
                <div class="stat-value" id="coins">100</div>
            </div>
        </div>

        <div class="game-board" id="gameBoard">
            <!-- 猫咪槽位将通过JavaScript动态生成 -->
        </div>

        <div class="controls">
            <button class="btn" onclick="addRandomCat()">添加猫咪 (50金币)</button>
            <button class="btn" onclick="resetGame()">重置游戏</button>
        </div>
    </div>

    <!-- 设置模态框 -->
    <div class="modal" id="settingsModal">
        <div class="modal-content">
            <span class="close" onclick="closeSettings()">&times;</span>
            <h2>游戏设置</h2>
            <button class="btn" onclick="exportSave()">导出存档</button>
            <button class="btn" onclick="importSave()">导入存档</button>
            <button class="btn" onclick="resetGame()">重置进度</button>
        </div>
    </div>

    <!-- 离线收益提示 -->
    <div class="offline-earnings" id="offlineEarnings">
        <h3>离线收益</h3>
        <p>你获得了 <span id="offlineCoins">0</span> 金币！</p>
        <button class="btn" onclick="closeOfflineEarnings()">领取</button>
    </div>

    <script>
        class CatMergeGame {
            constructor() {
                this.boardSize = 12;
                this.board = new Array(this.boardSize).fill(null);
                this.coins = 100;
                this.maxLevel = 1;
                this.lastSaveTime = Date.now();
                this.selectedIndex = -1;
                
                this.init();
                this.loadGame();
                this.checkOfflineEarnings();
                this.startAutoSave();
                this.startCoinGeneration();
            }

            init() {
                this.createBoard();
                this.updateUI();
                
                // 初始赠送两只1级猫
                if (!this.hasAnyCats()) {
                    this.addCat(0, 1);
                    this.addCat(1, 1);
                }
            }

            hasAnyCats() {
                return this.board.some(cat => cat !== null);
            }

            createBoard() {
                const gameBoard = document.getElementById('gameBoard');
                gameBoard.innerHTML = '';
                
                for (let i = 0; i < this.boardSize; i++) {
                    const slot = document.createElement('div');
                    slot.className = 'cat-slot';
                    slot.dataset.index = i;
                    slot.onclick = () => this.selectCat(i);
                    gameBoard.appendChild(slot);
                }
            }

            selectCat(index) {
                if (this.board[index] === null) return;

                if (this.selectedIndex === -1) {
                    this.selectedIndex = index;
                    document.querySelectorAll('.cat-slot')[index].classList.add('selected');
                } else if (this.selectedIndex === index) {
                    document.querySelectorAll('.cat-slot')[index].classList.remove('selected');
                    this.selectedIndex = -1;
                } else {
                    this.tryMerge(this.selectedIndex, index);
                }
            }

            tryMerge(index1, index2) {
                const cat1 = this.board[index1];
                const cat2 = this.board[index2];

                if (cat1 && cat2 && cat1.level === cat2.level) {
                    // 执行合并动画
                    this.animateMerge(index1, index2, cat1.level + 1);
                    
                    // 合并逻辑
                    this.board[index1] = null;
                    this.board[index2] = { level: cat1.level + 1 };
                    
                    this.maxLevel = Math.max(this.maxLevel, cat1.level + 1);
                    this.coins += cat1.level * 10;
                    
                    this.selectedIndex = -1;
                    document.querySelectorAll('.cat-slot').forEach(slot => slot.classList.remove('selected'));
                    
                    this.updateUI();
                    this.saveGame();
                } else {
                    // 取消选择
                    document.querySelectorAll('.cat-slot')[index1].classList.remove('selected');
                    this.selectedIndex = index2;
                    document.querySelectorAll('.cat-slot')[index2].classList.add('selected');
                }
            }

            animateMerge(index1, index2, newLevel) {
                const slots = document.querySelectorAll('.cat-slot');
                
                // 添加合并动画
                slots[index1].querySelector('.cat')?.classList.add('merge-animation');
                slots[index2].querySelector('.cat')?.classList.add('merge-animation');
                
                setTimeout(() => {
                    this.renderBoard();
                    if (this.board[index2]) {
                        slots[index2].querySelector('.cat')?.classList.add('new-cat-animation');
                    }
                }, 500);
            }

            addCat(index, level = 1) {
                if (index === undefined) {
                    const emptySlots = this.board.map((cat, i) => cat === null ? i : -1).filter(i => i !== -1);
                    if (emptySlots.length === 0) return false;
                    index = emptySlots[Math.floor(Math.random() * emptySlots.length)];
                }
                
                if (this.board[index] === null) {
                    this.board[index] = { level: level };
                    this.updateUI();
                    this.saveGame();
                    return true;
                }
                return false;
            }

            addRandomCat() {
                if (this.coins >= 50) {
                    const success = this.addCat();
                    if (success) {
                        this.coins -= 50;
                        this.updateUI();
                        this.saveGame();
                    }
                }
            }

            renderBoard() {
                const slots = document.querySelectorAll('.cat-slot');
                slots.forEach((slot, index) => {
                    slot.innerHTML = '';
                    slot.classList.remove('selected');
                    
                    if (this.board[index]) {
                        const cat = document.createElement('div');
                        cat.className = 'cat';
                        
                        // 根据等级设置颜色和表情
                        const colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FECA57', '#FF9FF3', '#54A0FF'];
                        const emojis = ['😺', '😸', '😹', '😻', '😼', '😽', '🙀', '😿', '😾'];
                        
                        const colorIndex = (this.board[index].level - 1) % colors.length;
                        const emojiIndex = (this.board[index].level - 1) % emojis.length;
                        
                        cat.style.backgroundColor = colors[colorIndex];
                        cat.textContent = emojis[emojiIndex];
                        
                        const level = document.createElement('div');
                        level.className = 'cat-level';
                        level.textContent = this.board[index].level;
                        
                        cat.appendChild(level);
                        slot.appendChild(cat);
                    }
                });
            }

            updateUI() {
                document.getElementById('maxLevel').textContent = this.maxLevel;
                document.getElementById('totalCats').textContent = this.board.filter(cat => cat !== null).length;
                document.getElementById('coins').textContent = this.coins;
                this.renderBoard();
            }

            saveGame() {
                const saveData = {
                    board: this.board,
                    coins: this.coins,
                    maxLevel: this.maxLevel,
                    lastSaveTime: Date.now()
                };
                localStorage.setItem('catMergeSave', JSON.stringify(saveData));
            }

            loadGame() {
                const saveData = localStorage.getItem('catMergeSave');
                if (saveData) {
                    const data = JSON.parse(saveData);
                    this.board = data.board || new Array(this.boardSize).fill(null);
                    this.coins = data.coins || 100;
                    this.maxLevel = data.maxLevel || 1;
                    this.lastSaveTime = data.lastSaveTime || Date.now();
                }
            }

            checkOfflineEarnings() {
                const now = Date.now();
                const timeDiff = now - this.lastSaveTime;
                const hoursOffline = timeDiff / (1000 * 60 * 60);
                
                if (hoursOffline > 0.1) {
                    const offlineCoins = Math.floor(hoursOffline * 10 * this.maxLevel);
                    this.coins += offlineCoins;
                    
                    if (offlineCoins > 0) {
                        document.getElementById('offlineCoins').textContent = offlineCoins;
                        document.getElementById('offlineEarnings').style.display = 'block';
                    }
                    
                    this.saveGame();
                }
            }

            startAutoSave() {
                setInterval(() => {
                    this.saveGame();
                }, 10000);
            }

            startCoinGeneration() {
                setInterval(() => {
                    const catCount = this.board.filter(cat => cat !== null).length;
                    if (catCount > 0) {
                        this.coins += Math.floor(catCount * this.maxLevel / 10);
                        this.updateUI();
                    }
                }, 5000);
            }

            resetGame() {
                if (confirm('确定要重置游戏进度吗？这将删除所有数据！')) {
                    this.board = new Array(this.boardSize).fill(null);
                    this.coins = 100;
                    this.maxLevel = 1;
                    this.selectedIndex = -1;
                    
                    this.addCat(0, 1);
                    this.addCat(1, 1);
                    
                    this.updateUI();
                    this.saveGame();
                    closeSettings();
                }
            }

            exportSave() {
                const saveData = {
                    board: this.board,
                    coins: this.coins,
                    maxLevel: this.maxLevel,
                    lastSaveTime: Date.now()
                };
                
                const dataStr = JSON.stringify(saveData, null, 2);
                const dataBlob = new Blob([dataStr], { type: 'application/json' });
                const url = URL.createObjectURL(dataBlob);
                
                const a = document.createElement('a');
                a.href = url;
                a.download = 'cat-merge-save.json';
                a.click();
                
                URL.revokeObjectURL(url);
                alert('存档已导出！');
            }

            importSave() {
                const input = document.createElement('input');
                input.type = 'file';
                input.accept = '.json';
                
                input.onchange = (e) => {
                    const file = e.target.files[0];
                    if (file) {
                        const reader = new FileReader();
                        reader.onload = (e) => {
                            try {
                                const data = JSON.parse(e.target.result);
                                this.board = data.board || new Array(this.boardSize).fill(null);
                                this.coins = data.coins || 100;
                                this.maxLevel = data.maxLevel || 1;
                                
                                this.updateUI();
                                this.saveGame();
                                alert('存档已导入！');
                                closeSettings();
                            } catch (error) {
                                alert('导入失败：无效的存档文件！');
                            }
                        };
                        reader.readAsText(file);
                    }
                };
                
                input.click();
            }
        }

        // 全局函数
        let game;

        function initGame() {
            game = new CatMergeGame();
        }

        function addRandomCat() {
            game.addRandomCat();
        }

        function resetGame() {
            game.resetGame();
        }

        function openSettings() {
            document.getElementById('settingsModal').style.display = 'block';
        }

        function closeSettings() {
            document.getElementById('settingsModal').style.display = 'none';
        }

        function exportSave() {
            game.exportSave();
        }

        function importSave() {
            game.importSave();
        }

        function closeOfflineEarnings() {
            document.getElementById('offlineEarnings').style.display = 'none';
        }

        // 点击模态框外部关闭
        window.onclick = function(event) {
            const modal = document.getElementById('settingsModal');
            if (event.target === modal) {
                modal.style.display = 'none';
            }
        }

        // 初始化游戏
        document.addEventListener('DOMContentLoaded', initGame);
    </script>
</body>
</html>
